package com.example.tellwin.home.act;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.donkingliang.labels.LabelsView;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.adpater.KnowledgeAdapter;
import com.example.tellwin.home.bean.HistoryBean;
import com.example.tellwin.home.bean.HotSearchBean;
import com.example.tellwin.home.bean.QuestionBean;
import com.example.tellwin.home.contract.SearchContract;
import com.example.tellwin.home.presenter.SearchPresenter;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.ToastUtil;
import com.example.tellwin.view.ShareDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends CpBaseActivty implements SearchContract.View {

    @BindView(R.id.search_resutl_ll)
    LinearLayout searchResutlLl;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_btn)
    TextView searchBtn;
    @BindView(R.id.search_rv)
    RecyclerView searchRv;

    @BindView(R.id.history_record_lv)
    LabelsView historyRecordLv;
    @BindView(R.id.hot_search_lv)
    LabelsView hotSearchLv;
    @BindView(R.id.normal_ll)
    LinearLayout normalLl;

    @BindView(R.id.none_data_ll)
    LinearLayout noneDataLl;

    KnowledgeAdapter knowledgeAdapter;
    List<QuestionBean> questionBeans = new ArrayList<>();
    List<HistoryBean> historys = new ArrayList<>();
    List<HotSearchBean> hotSearchBeans = new ArrayList<>();

    @Inject
    SearchPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        searchRv.setLayoutManager(linearLayoutManager);
        searchRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_15);
            }
        });
        knowledgeAdapter = new KnowledgeAdapter();
        searchRv.setAdapter(knowledgeAdapter);


    }

    @Override
    protected void initEvents() {
        historyRecordLv.setOnLabelClickListener((label, data, position) -> {
            setSearchEt(historys.get(position).getSearchName());
        });
        hotSearchLv.setOnLabelClickListener((label, data, position) -> {
            setSearchEt(hotSearchBeans.get(position).getSearchName());
        });
        knowledgeAdapter.setKnowledgeListener(new KnowledgeAdapter.KnowledgeListener() {
            @Override
            public void onItemLikeClick(int position, QuestionBean questionBean) {
                mPresenter.likeQuestion(questionBean.getQuestionId(), position);
            }

            @Override
            public void onItemShareClick(int position, QuestionBean questionBean) {
                ShareDialog shareDialog = new ShareDialog(SearchActivity.this ,2 ,questionBean.getQuestionId(),questionBean.getQuestionName());
                shareDialog.show();
            }

            @Override
            public void onItemClick(int position, QuestionBean questionBean) {
                Intent intent = new Intent(SearchActivity.this , QuestionDetailActivity.class);
                intent.putExtra(Common.QUESTION , questionBean.getQuestionId());
                startActivity(intent);
            }
        });
        searchRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(CommonUtils.isSlideToBottom(searchRv)) {
                    mPresenter.loadMore();
                }
            }
        });
    }

    @Override
    protected void initDatas() {
        mPresenter.historySearch();
        mPresenter.hotSearch();
    }

    private void setSearchEt(String str) {
        searchEt.setText(str);
        mPresenter.search(str);
    }

    @OnClick(R.id.search_btn)
    public void onViewClicked() {
        String searchStr = searchEt.getText().toString();
        if (searchStr.length() == 0) {
            ToastUtil.show(this, R.string.home_search_hint);
            return;
        }
        mPresenter.search(searchStr);
    }

    @Override
    public void historySearchResult(List<HistoryBean> historyBeans) {
        historys = historyBeans;
        historyRecordLv.setLabels(historys, (label, position, data) -> data.getSearchName());
    }

    @Override
    public void hotSearchResult(List<HotSearchBean>hotSearchBeans) {
        this.hotSearchBeans = hotSearchBeans;
        hotSearchLv.setLabels(hotSearchBeans, (label, position, data) -> data.getSearchName());
    }

    @Override
    public void searchResult(List<QuestionBean> searchList) {
        normalLl.setVisibility(View.GONE);
        searchResutlLl.setVisibility(View.VISIBLE);
        noneDataLl.setVisibility(View.GONE);
        this.questionBeans = searchList;
        knowledgeAdapter.setKnowdegeBeans(questionBeans);
    }

    @Override
    public void searchFail() {
        noneDataLl.setVisibility(View.VISIBLE);
        normalLl.setVisibility(View.GONE);
    }

    @Override
    public void loadMoreResult(List<QuestionBean> searchList) {
        this.questionBeans.addAll(searchList);
        knowledgeAdapter.notifyDataSetChanged();
    }

    @Override
    public void searchNotMoreData() {

    }

    @Override
    public void likeQuestionResult(int position) {
        int like = questionBeans.get(position).getIsLike();
        if (like == 0) {
            like = 1;
        } else {
            like = 0;
        }
        questionBeans.get(position).setIsLike(like);
        knowledgeAdapter.notifyDataSetChanged();
    }
}
